package com.csust.automaticteach.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.csust.automaticteach.common.PageRequest;
import com.csust.automaticteach.exception.BusinessException;
import com.csust.automaticteach.exception.ErrorCode;
import com.csust.automaticteach.model.entity.*;
import com.csust.automaticteach.model.vo.exercises.StudentExercisesSetsVO;
import com.csust.automaticteach.model.vo.user.UserVO;
import com.csust.automaticteach.service.*;
import com.csust.automaticteach.mapper.ClassExerciseMapper;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author 钰玟
* @description 针对表【t_class_exercise(班级练习关联表)】的数据库操作Service实现
* @createDate 2025-08-16 15:41:14
*/
@Service
public class ClassExerciseServiceImpl extends ServiceImpl<ClassExerciseMapper, ClassExercise>
    implements ClassExerciseService{

    @Resource
    @Lazy
    private ClassesService classesService;
    @Resource
    private ExercisesSetsService exercisesSetsService;
    @Resource
    private ClassStudentService classStudentService;
    @Resource
    private ClassTeacherService classTeacherService;
    @Resource
    private ClassExerciseMapper classExerciseMapper;
    @Resource
    private UserService userService;
    /**
     * (班主任)根据教师id获取该教师创建的班级练习
     * @param teacherId 教师id 如果该教师是headTeacher 则可以获取全部的班级练习
     * @return 创建的班级练习
     */
    @Override
    public List<ExercisesSets> listExerciseSetsByTeacherId(Long teacherId) {
        //1.根据班级-教师 表先拿到班级id
        QueryWrapper<Classes> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("headTeacher", teacherId);
        Classes ClassByTeacherId = classesService.getOne(queryWrapper);
        if(ClassByTeacherId == null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"没有该老师对应的班级");
        }
        Long classId = ClassByTeacherId.getId();
        //2. 根据该班级id 获取班级下的 exercisesId
        QueryWrapper<ClassExercise> query = new QueryWrapper<>();
        query.eq("classId", classId);
        List<Long> exercisesSetsIds = this.list(query).stream().map(ClassExercise::getExercisesSetsId).toList();
        //3. 根据 exercisesSetsIds 获取 exercisesSets
        return exercisesSetsService.listByIds(exercisesSetsIds);
    }
    /**
     * 根据学生id获取该学生加入的班级练习
     * @param pageRequest 分页参数
     * @param studentId 学生id
     * @return 加入的班级练习
     */
    @Override
    public Page<StudentExercisesSetsVO> listExerciseSetsByStudentId(PageRequest pageRequest, Long studentId) {
        //1.先拿到班级id
        QueryWrapper<ClassStudent> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("studentId", studentId);
        ClassStudent classStudent = classStudentService.getOne(queryWrapper);
        if(classStudent == null){
            throw new BusinessException(ErrorCode.OPERATION_ERROR,"没有该学生对应的班级");
        }
        Long classId = classStudent.getClassId();
        //2. 根据该班级id 获取班级下的 所有练习 StudentExercisesSetsVO
        //todo select exercisesSetsId,status,publishTime,endTime,title,publishTeacher
        // from t_class_exercise as tce,t_exercises_sets as tes where tce.classId = ? and tce.isDelete = 0
        // and tes.id = tce.exercisesSetsId and tes.isDelete = 0
        Page<StudentExercisesSetsVO> studentExercisesSetsVOPage = new Page<>(pageRequest.getCurrent(), pageRequest.getPageSize());
        studentExercisesSetsVOPage = classExerciseMapper.listStudentExercisesSetsVO(studentExercisesSetsVOPage,classId);
        List<StudentExercisesSetsVO> list = studentExercisesSetsVOPage.getRecords().stream().map(
                studentExercisesSetsVO -> {
                    User user = userService.getById(studentExercisesSetsVO.getPublishTeacher());
                    studentExercisesSetsVO.setUserName(user.getUserName());
                    return studentExercisesSetsVO;
                }
        ).toList();
        studentExercisesSetsVOPage.setRecords(list);
        return studentExercisesSetsVOPage;
    }
}




